package org.example.projecttwo.controller.content;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.example.projecttwo.common.Result;
import org.example.projecttwo.entity.Comment;
import org.example.projecttwo.entity.dto.content.request.CommentDTO;
import org.example.projecttwo.entity.dto.content.request.CommentQueryDTO;
import org.example.projecttwo.entity.dto.content.request.ContentWithCommentCountDTO;
import org.example.projecttwo.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/comments")  // 修改路径，区分管理端接口
@Tag(name = "评论管理", description = "评论信息管理")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @GetMapping("/list")
    @Operation(summary = "分页查询评论列表", description = "分页查询评论列表，支持多条件筛选 -- 评论管理")
    public Result<IPage<Comment>> listComments(CommentQueryDTO queryDTO,
                                               @RequestParam(defaultValue = "1") long current,
                                               @RequestParam(defaultValue = "10") long size) {  // 调整默认分页大小为10，更符合管理界面
        Page<Comment> page = new Page<>(current, size);
        IPage<Comment> result = commentService.pageByQuery(page, queryDTO);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取评论详情", description = "根据评论ID获取评论详情 -- 评论管理")
    public Result<Comment> getComment(@PathVariable Long id) {
        Comment comment = commentService.getById(id);
        if (comment == null) {
            return Result.error("评论不存在");
        }
        return Result.success(comment);
    }


    @DeleteMapping("/{id}")
    @Operation(summary = "删除单条评论", description = "根据评论ID删除评论，同时删除其下所有子评论 -- 评论管理")
    public Result<String> deleteComment(@PathVariable Long id) {
        // 检查评论是否存在
        if (!commentService.existsById(id)) {
            return Result.error("评论不存在");
        }
        // 执行删除（包括子评论）
        int deleteCount = commentService.removeCommentAndChildren(id);
        return Result.success("删除成功，共删除" + deleteCount + "条评论");
    }

    @PostMapping("/batch-delete")
    @Operation(summary = "批量删除评论", description = "批量删除选中的评论，包括其下所有子评论 -- 评论管理")
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择至少一条评论");
        }
        int deleteCount = commentService.removeCommentsAndChildren(ids);
        return Result.success("批量删除成功，共删除" + deleteCount + "条评论");
    }


    @DeleteMapping("/clear")
    @Operation(summary = "清空内容评论", description = "删除指定内容的所有评论（包括所有层级） -- 评论管理")
    public Result<String> clearContentComments(
            @RequestParam Integer contentType,
            @RequestParam Long contentId) {
        // 验证内容类型（1=文章，2=新闻）
        if (contentType != 1 && contentType != 2) {
            return Result.error("内容类型无效，1=文章，2=新闻");
        }
        int deleteCount = commentService.clearByContentTypeAndId(contentType, contentId);
        return Result.success("内容评论已清空，共删除" + deleteCount + "条评论");
    }

    @GetMapping("/content/list")
    @Operation(summary = "查询内容列表（带评论数）", description = "查询文章或新闻列表，包含其评论数量 -- 评论管理")
    public Result<IPage<ContentWithCommentCountDTO>> listContentWithCommentCount(
            @RequestParam Integer contentType,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        // 验证内容类型（1=文章，2=新闻）
        if (contentType != 1 && contentType != 2) {
            return Result.error("内容类型无效，1=文章，2=新闻");
        }
        Page<ContentWithCommentCountDTO> page = new Page<>(current, size);
        IPage<ContentWithCommentCountDTO> result = commentService.pageContentWithCommentCount(page, contentType, keyword);
        return Result.success(result);
    }

    @GetMapping("/{id}/replies")
    @Operation(summary = "获取评论回复", description = "查询指定评论的所有回复 -- 评论管理")
    public Result<IPage<Comment>> getCommentReplies(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        Page<Comment> page = new Page<>(current, size);
        IPage<Comment> replies = commentService.pageReplies(page, id);
        return Result.success(replies);
    }
}